package nettyteach.endecoder;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * @Author ligengxian
 * @Date 2022/4/24 下午9:06
 * @Version 1.0
 * @Description: 客户端hadnler初始化配置类，在channel注册到eventloop后，对这个channel添加一些初始化的handler
 **/
public class InitializerClientEndeCoder extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline=socketChannel.pipeline();
        //定长解码
//        pipeline.addLast("framer",new FixedLengthFrameDecoder(CommonTools.FIXEDLENGTHFRAME_LENGTH));
        //行解码器
        pipeline.addLast("framer",new LineBasedFrameDecoder(CommonTools.LINEBASEDFRAME_KENGTH,true,true));


        pipeline.addLast("decoder",new StringDecoder());
        pipeline.addLast("encoder",new StringEncoder());


        //自定义业务逻辑handler
//        pipeline.addLast(new HandlerClientFixedLength());
        pipeline.addLast(new HandlerClientLineBased());
    }
}
